package com.wang.myblog.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回结果对象
 */
@Data
public class R {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();

    public static Integer SUCCESS = 200;
    /**
     * 添加成功
     */
    public static Integer ADD = 201;
    /**
     * 删除成功
     */
    public static Integer DELETED = 204;
    /**
     * 请求的地址不存在或者包含不支持的参数
     */
    public static Integer BADREQUEST = 400;
    /**
     * 未授权
     */
    public static Integer UNAUTHORIZED = 401;
    /**
     * 账号已冻结
     */
    public static Integer frozen = 402;
    /**
     * 没有权限
     */
    public static Integer noPermission = 403;
    /**
     * 请求的资源不存在
     */
    public static Integer NOTFOUND = 404;
    /**
     * 内部错误
     */
    public static Integer ERROR = 500;
    /**
     * 非法操作
     */
    public static Integer illegal = 501;

    //构造方法私有化，调用只能调用公共的静态的方法（链式编程）
    private R() {
    }

    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(R.SUCCESS);
        r.setMessage("成功");
        return r;
    }

    public static R ok(Integer code, String message) {
        R r = new R();
        r.setSuccess(true);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(R.ERROR);
        r.setMessage("失败");
        return r;
    }

    public static R error(String message) {
        R r = new R();
        r.setSuccess(false);
        r.setCode(R.ERROR);
        r.setMessage(message);
        return r;
    }

    public static R error(Integer code, String message) {
        R r = new R();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    /**
     * 未授权错误
     *
     * @return unauthorizedError
     */
    public static R UNAUTHORIZED() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(R.noPermission);
        r.setMessage("您没有权限访问。");
        return r;
    }

    public static R add(boolean save) {
        R r = new R();
        if (save) {
            r.setSuccess(true);
            r.setCode(R.ADD);
            r.setMessage("添加成功");
        } else {
            r.setSuccess(false);
            r.setCode(R.ERROR);
            r.setMessage("添加失败");
        }
        return r;
    }

    public static R remove(boolean remove) {
        R r = new R();
        if (remove) {
            r.setSuccess(true);
            r.setCode(R.DELETED);
            r.setMessage("删除成功");
        } else {
            r.setSuccess(false);
            r.setCode(R.ERROR);
            r.setMessage("删除失败");
        }
        return r;
    }

    public static R update(boolean update) {
        R r = new R();
        if (update) {
            r.setSuccess(true);
            r.setCode(R.SUCCESS);
            r.setMessage("修改成功");
        } else {
            r.setSuccess(false);
            r.setCode(R.ERROR);
            r.setMessage("修改失败");
        }
        return r;
    }

    public static R update(boolean update, String msg) {
        R r = new R();
        if (update) {
            r.setSuccess(true);
            r.setCode(R.SUCCESS);
            r.setMessage(msg + "成功");
        } else {
            r.setSuccess(false);
            r.setCode(R.ERROR);
            r.setMessage(msg + "失败");
        }
        return r;
    }

    public R success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}
